<template>
  <div class="managePage">
    <el-menu
      :default-active="activeIndex"
      mode="horizontal"
      @select="handleSelect"
    >
      <el-menu-item :index="tab.key" v-for="tab in tabs">{{
        tab.title
      }}</el-menu-item>
    </el-menu>
    <div class="content">
      <component
        ref="contentRef"
        :is="activeIndex"
        :isAdmin="isAdmin"
      ></component>
    </div>
  </div>
</template>

<script>
import bert from "./bert/index.vue";
import module from "./module/index.vue";
import oneNet from "./oneNet/index.vue";
import relearn from "./relearn/index.vue";
import schoolItem from "./schoolItem/index.vue";
import standard from "./standard/index.vue";
import workBook from "./workBook/index.vue";

export default {
  components: { standard, oneNet, relearn, workBook, module, bert, schoolItem },
};
</script>

<script setup>
import { nextTick, onMounted, ref } from "vue";

import { api_getBaseUrl, api_padiqsx, setConfigByManage } from "@/api/manage";
import { ElMessage } from "element-plus";
import {
  getCurSchoolInfo,
  getSubSystemsMainServerBySubjectID,
  setCommonBaseUrl,
  setCommonConfig,
} from "../../utils/CommonRequest.js";

const tabs = [
  {
    key: "standard",
    title: "系统标准指令",
  },
  {
    key: "oneNet",
    title: "一网通办",
  },
  {
    key: "relearn",
    title: "指令再学习",
  },
  {
    key: "workBook",
    title: "部门职责",
  },
  {
    key: "module",
    title: "模块核对",
  },
  {
    key: "bert",
    title: "模型数据",
  },
  {
    key: "schoolItem",
    title: "校园数据",
  },
];
const activeIndex = ref(tabs[0].key);
const contentRef = ref(null);

const baseUrl = ref(""); //基础平台地址
const websocketPort = ref(""); //websocket端口号
const robotAddr = ref(""); //机器人后台服务地址
const schoolId = ref(""); //地址栏获取的schoolId
const lg_tk = ref(""); //地址栏获取的lg_tk
const userId = ref(""); //地址栏获取的userId
const websocket = ref(null); //websocket对象
const title = ref("");
const isAdmin = ref(false);

onMounted(async () => {
  // 1、获取地址栏的信息
  lg_tk.value = getDataType("lg_tk");
  let password = getDataType("password");
  let onlineUserInfo = null;

  // 2、从后台获取基础平台地址和用户信息
  await api_getBaseUrl(lg_tk.value)
    .then((res) => {
      console.log("manage.vue获取用户信息 = ", res);

      baseUrl.value = res.data.webSrvAddr;
      onlineUserInfo = res;
    })
    .catch((err) => {
      console.log("manage.vue获取后台基础平台出错");
    });

  // 3、解析用户信息
  let userInfo = onlineUserInfo?.data || "";
  schoolId.value =
    userInfo?.SchoolID ||
    userInfo?.SchoolId ||
    userInfo?.schoolID ||
    userInfo?.schoolId;
  userId.value = userInfo?.userId || userInfo?.UserID;

  // 4、设置基础平台地址，为【获取子系统地址、学校信息】等方法服务
  setCommonBaseUrl(baseUrl.value);
  setCommonConfig({
    schoolId: schoolId.value,
  });

  // 5、获取机器人后台地址
  let robotRes = await getSubSystemsMainServerBySubjectID("E82");
  robotAddr.value = robotRes?.Data[0]?.WsSvrAddr; //后台服务地址
  console.log("Manage.vue 后台地址 = ", robotAddr.value);

  let schoolInfo = await getCurSchoolInfo();

  // 6、传递用户信息
  setConfigByManage({
    userId: userId.value,
    schoolId: schoolId.value,
    token: lg_tk.value,
    addr: robotAddr.value,
    userName: decodeURIComponent(userInfo?.UserName),
    version: schoolInfo?.SchoolLevel,
  });

  let padiqsxRes = null;
  await api_padiqsx()
    .then((res) => {
      console.log("api_padiqsx res = ", res);
      padiqsxRes = res;
    })
    .catch((err) => {
      console.log("api_padiqsx err = ", err);
    });

  if ((padiqsxRes.data && padiqsxRes.data == true) || password == "pwd888888") {
    // 通知子组件[Info.vue]和[EditInstruction]组件
    contentRef.value.refresh();
    contentRef.value.init();
    isAdmin.value = true;
  } else {
    ElMessage.info("无权查看该页面");
    isAdmin.value = false;
  }
});

const getDataType = (type) => {
  const urlParams = new URLSearchParams(window.location.search);
  let lg_tk = urlParams.get(type);
  if (!lg_tk) {
    const hashParams = new URLSearchParams(window.location.hash.split("?")[1]);
    lg_tk = hashParams.get(type);
  }
  if (lg_tk && lg_tk?.endsWith("/")) {
    lg_tk = lg_tk.slice(0, -1);
  }

  return lg_tk;
};

const handleSelect = (key) => {
  activeIndex.value = key;
  nextTick(() => {
    contentRef.value.refresh();
    contentRef.value.init();
  });
};
</script>

<style lang="scss" scoped>
.managePage {
  min-width: 1720px;
  box-sizing: border-box;
  .content {
    height: calc(100vh - 60px);
  }

  li {
    font-size: 18px;
    font-weight: 600;

    padding: 0px 34px;
  }
}
</style>
